package com.business.business.web.service;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.business.business.common.base.BaseService;
import com.business.business.model.BusinessBasisinfoFiles;
import com.business.business.model.BusinessFirepreventionplan;
import com.business.business.support.model.RespEntity;
import com.business.business.support.mybatisplus.QueryWrapper;
import com.business.business.support.utils.requestutil.HttpRequestUtil;
import com.business.business.web.mapper.BusinessBasisinfoFilesMapper;
import com.business.business.web.mapper.BusinessFirepreventionplanMapper;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
@Slf4j
public class FirepreventionplanService extends BaseService {
    private static final Logger logger = LoggerFactory.getLogger(FirepreventionplanService.class);
    @Resource
    private BusinessFirepreventionplanMapper businessFirepreventionplanMapper;
    @Resource
    private BusinessBasisinfoFilesMapper businessBasisinfoFilesMapper;

    public RespEntity firepreventionplanAdd(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());

        com.alibaba.fastjson.JSONObject token = makeDataForToken(jsonParam,"systemsetup","config","getDecodeToken","systemsetup");
        //调用token验证接口
        if(StrUtil.isNotBlank(token.getString("methodCodeFlag"))) return RespEntity.expire();
        if(StrUtil.isNotBlank(token.getString("methodErrorFlag"))) return RespEntity.fail(token.getString("msg"));
        String home_id = token.getString("home_id");
        String userId = token.getString("user_id");
        String userType = token.getString("user_type");
        String businessId = "";
        if ("1".equals(userType)){
            businessId = jsonParam.getStr("businessId");
        }else {
            businessId = home_id;
        }
        //获取前台的数据
        String name = jsonParam.getStr("name");
        String briefly = jsonParam.getStr("briefly");
        String fileName = jsonParam.getStr("fileName");
        String filePath = jsonParam.getStr("filePath");
        //自动生成ID
        String uuid = UUID.randomUUID().toString();
        //自动生成maketime
        Date date=new Date();
//        String makingTime = DateUtil.now();
        String createyear = DateUtil.year(date)+"";
        BusinessFirepreventionplan businessFirepreventionplan = new BusinessFirepreventionplan();
        businessFirepreventionplan.setId(uuid);
        businessFirepreventionplan.setName(name);
        businessFirepreventionplan.setBusinessId(businessId);
        businessFirepreventionplan.setBriefly(briefly);
        businessFirepreventionplan.setMakingtime(date);
        businessFirepreventionplan.setCreateyear(createyear);
        boolean businessFirepreventionplanInsert = businessFirepreventionplanMapper.insert(businessFirepreventionplan)>0?true:false;
            BusinessBasisinfoFiles businessBasisinfoFiles = new BusinessBasisinfoFiles();
            businessBasisinfoFiles.setBusinessBasisinfoId(uuid);
            businessBasisinfoFiles.setFileName(fileName);
            businessBasisinfoFiles.setFilePath(filePath);
        boolean insertBusinessBasisinfoFiles = businessBasisinfoFilesMapper.insert(businessBasisinfoFiles)>0?true:false;

        if (insertBusinessBasisinfoFiles && insertBusinessBasisinfoFiles) {
            return RespEntity.SUCCESS;
        } else {
            return RespEntity.fail("插入失败");
        }
    }

    /**
     * @Author LiHaoDong
     * @Description 消防预案列表
     * @Date 17:54 2020/7/22
     * @Param [jsonParam]
     * @return com.business.business.support.model.RespEntity
     **/
    public RespEntity firepreventionplanListPage(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());

        com.alibaba.fastjson.JSONObject token = makeDataForToken(jsonParam,"systemsetup","config","getDecodeToken","systemsetup");
        //调用token验证接口
        if(StrUtil.isNotBlank(token.getString("methodCodeFlag"))) return RespEntity.expire();
        if(StrUtil.isNotBlank(token.getString("methodErrorFlag"))) return RespEntity.fail(token.getString("msg"));
        String home_id = token.getString("home_id");
        String userId = token.getString("user_id");
        String userType = token.getString("user_type");
        if ("2".equals(userType)){
            jsonParam.put("businessId",home_id);
        }
        Page<BusinessFirepreventionplan> page = new Page<>(jsonParam.getInt("page"), jsonParam.getInt("limit"));
        IPage<BusinessFirepreventionplan> iPage = this.businessFirepreventionplanMapper.firepreventionplanListPage(page, jsonParam);
        List<BusinessFirepreventionplan> records = iPage.getRecords();

        Map<String, Object> data = new HashMap<>();
        data.put("totalSize", iPage.getTotal());
        data.put("pages", iPage.getPages());
        data.put("pageSize", jsonParam.getInt("limit"));
        data.put("Items",records);
        data.put("currentPage",jsonParam.getInt("page"));
        data.put("currentSize",jsonParam.getInt("limit"));
        return RespEntity.success(data);
    }

    /**
     * @Author LiHaoDong
     * @Description 消防预案详情
     * @Date 18:01 2020/7/22
     * @Param [jsonParam]
     * @return com.business.business.support.model.RespEntity
     **/
    public RespEntity firepreventionplanDetail(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());

        com.alibaba.fastjson.JSONObject token = makeDataForToken(jsonParam,"systemsetup","config","getDecodeToken","systemsetup");
        //调用token验证接口
        if(StrUtil.isNotBlank(token.getString("methodCodeFlag"))) return RespEntity.expire();
        if(StrUtil.isNotBlank(token.getString("methodErrorFlag"))) return RespEntity.fail(token.getString("msg"));
        String home_id = token.getString("home_id");

        String id = jsonParam.getStr("id");

        BusinessFirepreventionplan businessFirepreventionplans = this.businessFirepreventionplanMapper.firepreventionplanDetail(id);
        List<BusinessBasisinfoFiles> basisinfoFilesList = businessBasisinfoFilesMapper.selectList(new QueryWrapper<BusinessBasisinfoFiles>().eq("business_basisinfo_id",id));
        BusinessBasisinfoFiles basisinfoFiles= basisinfoFilesList.get(0);
        if (!businessFirepreventionplans.isEmpty()) {
            businessFirepreventionplans.put("fileInfo",basisinfoFiles);
            return RespEntity.success(businessFirepreventionplans);
        }
        return RespEntity.fail("查询失败");

    }

    /**
     * @Author LiHaoDong
     * @Description 消防预案新增
     * @Date 18:09 2020/7/22
     * @Param [jsonParam]
     * @return com.business.business.support.model.RespEntity
     **/
    public RespEntity fireprevfirepreventionplanAddentionplanDetail(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());

        com.alibaba.fastjson.JSONObject token = makeDataForToken(jsonParam,"systemsetup","config","getDecodeToken","systemsetup");
        //调用token验证接口
        if(StrUtil.isNotBlank(token.getString("methodCodeFlag"))) return RespEntity.expire();
        if(StrUtil.isNotBlank(token.getString("methodErrorFlag"))) return RespEntity.fail(token.getString("msg"));
        String home_id = token.getString("home_id");

        String briefly = jsonParam.getStr("briefly");
        String name = jsonParam.getStr("name");
        String fileName = jsonParam.getStr("fileName");
        String filePath = jsonParam.getStr("filePath");

        String id = UUID.randomUUID().toString();
        DateTime date = DateUtil.date();
        String year = DateUtil.year(date)+"";
        BusinessFirepreventionplan businessFirepreventionplan = new BusinessFirepreventionplan();
        businessFirepreventionplan.setId(id);
        businessFirepreventionplan.setName(name);
        businessFirepreventionplan.setMakingtime(date);
        businessFirepreventionplan.setBriefly(briefly);
        businessFirepreventionplan.setBusinessId(home_id);
        businessFirepreventionplan.setCreateyear(year);

        int insertFirepreventionplan = this.businessFirepreventionplanMapper.insert(businessFirepreventionplan);
        BusinessBasisinfoFiles businessBasisinfoFiles = new BusinessBasisinfoFiles();
        businessBasisinfoFiles.setBusinessBasisinfoId(id);
        businessBasisinfoFiles.setFileName(fileName);
        businessBasisinfoFiles.setFilePath(filePath);
        int insertBasisinfoFiles = this.businessBasisinfoFilesMapper.insert(businessBasisinfoFiles);
        if (insertFirepreventionplan > 0 && insertBasisinfoFiles > 0) {
            return RespEntity.success();
        }
        return RespEntity.fail("新增失败");
    }

    public RespEntity firepreventionplanUpdate(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());

        com.alibaba.fastjson.JSONObject token = makeDataForToken(jsonParam,"systemsetup","config","getDecodeToken","systemsetup");
        //调用token验证接口
        if(StrUtil.isNotBlank(token.getString("methodCodeFlag"))) return RespEntity.expire();
        if(StrUtil.isNotBlank(token.getString("methodErrorFlag"))) return RespEntity.fail(token.getString("msg"));
        String home_id = token.getString("home_id");

        String briefly = jsonParam.getStr("briefly");
        String name = jsonParam.getStr("name");
        String fileName = jsonParam.getStr("fileName");
        String filePath = jsonParam.getStr("filePath");
        String id = jsonParam.getStr("id");

        QueryWrapper queryWrapper = new QueryWrapper<BusinessBasisinfoFiles>();
        queryWrapper.eq("business_basisinfo_id", id);

        //删除
        int delete = this.businessBasisinfoFilesMapper.delete(queryWrapper);
        //插入
        BusinessBasisinfoFiles businessBasisinfoFiles = new BusinessBasisinfoFiles();
        businessBasisinfoFiles.setBusinessBasisinfoId(id);
        businessBasisinfoFiles.setFileName(fileName);
        businessBasisinfoFiles.setFilePath(filePath);
        int insert = this.businessBasisinfoFilesMapper.insert(businessBasisinfoFiles);

        //修改主表
        BusinessFirepreventionplan firepreventionplan = new BusinessFirepreventionplan();
        firepreventionplan.setId(id);
        firepreventionplan.setBriefly(briefly);
        firepreventionplan.setName(name);

        int update = this.businessFirepreventionplanMapper.updateById(firepreventionplan);
        if (delete > 0 && insert > 0 && update > 0) {
            return RespEntity.success();
        }
        return RespEntity.fail("更新失败");
    }

    /**
     * @Author LiHaoDong
     * @Description 消防预案删除
     * @Date 18:34 2020/7/22
     * @Param [jsonParam]
     * @return com.business.business.support.model.RespEntity
     **/
    public RespEntity firepreventionplanDelete(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());

        com.alibaba.fastjson.JSONObject token = makeDataForToken(jsonParam,"systemsetup","config","getDecodeToken","systemsetup");
        //调用token验证接口
        if(StrUtil.isNotBlank(token.getString("methodCodeFlag"))) return RespEntity.expire();
        if(StrUtil.isNotBlank(token.getString("methodErrorFlag"))) return RespEntity.fail(token.getString("msg"));
        String home_id = token.getString("home_id");

        String id = jsonParam.getStr("id");

        QueryWrapper queryWrapper = new QueryWrapper<BusinessBasisinfoFiles>();
        queryWrapper.eq("business_basisinfo_id", id);
        //删除
        int deleteBasisinfoFiles = this.businessBasisinfoFilesMapper.delete(queryWrapper);

        int deleteFirepreventionplan = this.businessFirepreventionplanMapper.deleteById(id);
        if (deleteBasisinfoFiles > 0 && deleteFirepreventionplan > 0) {
            return RespEntity.success();
        }
        return RespEntity.fail("删除失败");
    }
}
